package com.yc.bos.service.impl;

import com.yc.bos.dao.IRoleDao;
import com.yc.bos.model.Function;
import com.yc.bos.model.PageBean;
import com.yc.bos.model.Role;
import com.yc.bos.service.IRoleService;
import com.yc.bos.service.base.BaseServiceImpl;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService{
    @Autowired
    private IRoleDao roleDao;
    @Autowired
    private IdentityService identityService;

    @Override
    public void save(Role role, String functionIds) {
        //role:瞬时状态[no session,no id]
        //1.保存角色
        roleDao.save(role); //持久状态[session id]
        //2.把角色保存到activiti的组中
        Group group=new GroupEntity();
        group.setId(role.getName());
        group.setName(role.getName());
        identityService.saveGroup(group);
        //2.添加角色-权限 中间表数据
        //2.1 拆分id
        String[] functionIdsArr=functionIds.split(",");
        for(String  functionId:functionIdsArr){
            Function function=new Function();
            function.setId(functionId);
            role.getFunctions().add(function);//内部执行insert语句
        }
    }

    @Override
    public void pageQuery(PageBean<Role> pd) {
        roleDao.pageQuery(pd);
    }

    @Override
    public void save(Role entity) {

    }

    @Override
    public void update(Role entity) {

    }

    @Override
    public void delete(Role entity) {

    }

    @Override
    public Role findById(Serializable id) {
        return null;
    }

    @Override
    public List<Role> findAll() {
        return  roleDao.findAll();
    }
}
